package logic

import (
	"DataSrv/global/model"
	"context"

	"DataSrv/data"
	"DataSrv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type TypeFindLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTypeFindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TypeFindLogic {
	return &TypeFindLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TypeFindLogic) TypeFind(in *data.TypeFindRequest) (*data.TypeFindResponse, error) {
	// todo: add your logic here and delete this line
	var Datas []struct {
		Likes      int64
		View       int64
		VideoCount int64
		model.Type
	}
	model.DB.Debug().Model(&model.Type{}).Joins("join videos on videos.type_id = types.id join video_likes on video_likes.video_id = videos.id").Where("types.pid != 0").Group("types.id").Select("types.*,count(videos.id) as video_count,sum(videos.view_count) as view,count(video_likes.id) as likes").Find(&Datas)
	var datas []*data.FormType
	for _, s := range Datas {
		var data = &data.FormType{
			VideoCount: s.VideoCount,
			ViewCount:  s.View,
			LikeCount:  s.Likes,
			Name:       s.Name,
		}
		datas = append(datas, data)
	}
	return &data.TypeFindResponse{
		Types: datas,
	}, nil
}
